package org.wk.concurrent.app1;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
 * 通过JDK提供的CompletionService，将将要执行的任务和已经执行的任务分离开来。
 * 通过submit()方法提交任务，通过take()获得已经执行成功的任务。
 */
public class Client2 {
	public static void main(String[] args) throws InterruptedException,
			ExecutionException, TimeoutException {
		FetchList fetchList = new FetchList();
		FetchedList fetchedList = new FetchedList();
		List<File> results = new ArrayList<File>();
		ExecutorService exector = Executors.newFixedThreadPool(30);
		CompletionService<File> resultHolder = new ExecutorCompletionService<File>(exector);
		
		for (int i = 0; i < fetchList.size(); i++) {
			Crawler crawler = null;
			String url = fetchList.getUrl();

			// 检测这个url是否已经抓取过
			if (fetchedList.validate(url)) {
				crawler = new Crawler(url);
				fetchedList.addUrl(url);
			}

			if (!exector.isShutdown()) {
				resultHolder.submit(crawler);
			}
		}

		for (int i = 0; i < fetchList.size(); i++) {
			resultHolder.take().get(3, TimeUnit.SECONDS);
		}

		exector.shutdown();

		for (File f : results) {
			System.out.println(f.getName());
		}
	}
}
